package com.awspaass.user.apps.visitor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListener;
import com.actionsoft.bpms.bpmn.engine.listener.InterruptListenerInterface;
import com.actionsoft.bpms.commons.database.ColumnMapRowMapper;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.exception.BPMNError;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CommUtil;
import com.awspaas.user.apps.shhtaerospaceindustrial.util.CoreUtil;

public class EventEmployee extends InterruptListener implements InterruptListenerInterface {
	@Override
	public boolean execute(ProcessExecutionContext ctx) throws Exception {
		
		info("任务结束前可以阻止被触发-->" + ctx.getTaskInstance());
		String processInstId = ctx.getProcessInstance().getId();
		
		List<Map<String, Object>> appointList = DBSql.query("SELECT * FROM BO_EU_VISITOR_MANAGE WHERE BINDID = ? ", new ColumnMapRowMapper(), new Object[] {processInstId});
		if(appointList == null || appointList.isEmpty()) {
			throw new BPMNError("访客信息为空，请填写信息！");
		}

		
		for (Map<String, Object> map : appointList) {
			String inTime = CoreUtil.objToStr(map.get("INPARTTIME"));
			String outTime = CoreUtil.objToStr(map.get("OUTPARTTIME"));

			String visitType = CoreUtil.objToStr(map.get("VISITTYPE"));

			if (!inTime.equals(outTime) && !visitType.equals("短期")) {

				throw new BPMNError("请确认来访类型是否正确");
			}
			if (inTime.equals(outTime) && visitType.equals("短期")) {

				throw new BPMNError("请确认来访类型是否正确");
			}
			Timestamp in = new Timestamp(System.currentTimeMillis()); 
			Timestamp out = new Timestamp(System.currentTimeMillis()); 
			System.out.println(inTime+" 00:00:00");
			System.out.println(outTime+" 00:00:00");
	        try {  
	            in = Timestamp.valueOf(inTime+" 00:00:00"); 
	            out = Timestamp.valueOf(outTime+" 00:00:00");
	            if((out.getTime()/1000 -in.getTime()/1000)>4*24*3600) {
	            	throw new BPMNError("访问日期不能大于5天！");
	            }

	            
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
		}
		
		List<Map<String, Object>> userinfolist = DBSql.query("SELECT * FROM BO_EU_VISITOR_MANAGE_MX WHERE BINDID = ? ", new ColumnMapRowMapper(), new Object[] {processInstId});
		if(userinfolist == null || userinfolist.isEmpty()) {
			throw new BPMNError("人员明细为空，请添加人员信息后提交");
		}
		List<String> certlist=new ArrayList<String>();
		
		for (Map<String, Object> map : userinfolist) {
			String VISITORNAME = CoreUtil.objToStr(map.get("VISITORNAME"));
			String VISITORCELL = CoreUtil.objToStr(map.get("VISITORCELL"));
			
			String CERTNO = CoreUtil.objToStr(map.get("CERTNO"));
			if(!CommUtil.isIDNumber(CERTNO)) {
				throw new BPMNError("请核对来访人员身份证号格式！");
			}
			if(!CommUtil.matchPhoneNumber(VISITORCELL)) {
				throw new BPMNError("请核对手机号码格式！");
			}
			certlist.add(CERTNO);
//			String ABOUTFILE = CoreUtil.objToStr(map.get("ABOUTFILE"));
//			
//			if(ABOUTFILE=="") {
//				
//				throw new BPMNError("请确认访客："+VISITORNAME+"的证件照是否上传！");
//			}
			}
		long certnocount = certlist.stream().distinct().count();
		List dumplist = certlist.stream() 

				.collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) 

				.entrySet()

				.stream()

				.filter(e -> e.getValue() > 1) 

				.map(Map.Entry::getKey) 

				.collect(Collectors.toList());
		if(dumplist.size()>0) {
			String certstr= "";
			for(Object val:dumplist) {
				certstr=(String)val+";";
			}
			System.out.println(certstr);
			throw new BPMNError("来访人员中重复身份证号："+certstr); 
		}
		
		/*if(certnocount!=certlist.size()) {
			throw new BPMNError("来访人员信息中有重复身份证号！");
		}*/
		
		List<Map<String, Object>> carinfolist = DBSql.query("SELECT * FROM BO_EU_VISITOR_MANAGE_CARMX WHERE BINDID = ? ", new ColumnMapRowMapper(), new Object[] {processInstId});
		List<String> carnolist = new ArrayList<String>();
		
		if(carinfolist.size()>0) {
			for(Map<String, Object> map : carinfolist) {
				String CARNO = CoreUtil.objToStr(map.get("CARNO"));
				if(CARNO!="") {
					carnolist.add(CARNO);
				}
			}
			long carnocount = carnolist.stream().distinct().count();
			if(carnocount!=carnolist.size()) {
				throw new BPMNError("来访车辆信息里有重复车牌号！");
			}
		}
		
		return true;

	}
}
